<?php
	class Subject extends MY_controller{

		function __construct(){
			parent::__construct();
			
			$parent_id = 4;
			//获取school二级菜单
			$this->load->model('School_manage_menu_model');
		    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
		    // print_r($menu_list);exit;
		    $this->smarty->assign("menu_list", $menu_list);

			$this->left_menu_min = array("menu" => "题库管理", "curr" => '/subject', "list" => $menu_list);
		  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
		  	
			$this->smarty->assign("menu", 'tiku');
		}

		function index($title = 0, $tk_category_id = 0, $pack_status = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
				$tk_category_id = isset($post_data['tk_category_id']) ? $post_data['tk_category_id'] : 0;
				$pack_status = isset($post_data['pack_status']) ? $post_data['pack_status'] : 0;
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('title' => $title, "tk_category_id" => $tk_category_id, "school_id" => $this->user_data['school_id'], "pack_status" => $pack_status);
			$this->load->model("Subjects");
			$result = $this->Subjects->get_subject_list($limit, $this->offset, $conditions);


			$this->load->library('pagination');
			$page_config['base_url'] = '/subject/index/' . $title . '/' . $tk_category_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->load->model("Tk_categorys");
			$category_list = $this->Tk_categorys->get_top_categorys($this->user_data['school_id']);
			$this->smarty->assign("category_list", $category_list);
			$this->smarty->assign("action", '科目列表');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("subject_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("subject/list.html");
		}

		function system($title = 0, $tk_category_id = 0, $pack_status = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
				$tk_category_id = isset($post_data['tk_category_id']) ? $post_data['tk_category_id'] : 0;
				$pack_status = isset($post_data['pack_status']) ? $post_data['pack_status'] : 0;
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('title' => $title, "tk_category_id" => $tk_category_id, "school_id" => 0, "pack_status" => $pack_status);
			$this->load->model("Subjects");
			$result = $this->Subjects->get_subject_list($limit, $this->offset, $conditions);
			if($result['count'] > 0){
				foreach ($result['result'] as $key => $value) {
					$use_code = get_field_by_where("o2o_school_system_topic_codes", "COUNT(id) AS count", "subject_id='{$value['id']}' AND school_id='{$this->user_data['school_id']}' AND status = 2");
					$result['result'][$key]['use_code'] = $use_code['count'];
					$no_use_code = get_field_by_where("o2o_school_system_topic_codes", "COUNT(id) AS count", "subject_id='{$value['id']}' AND school_id='{$this->user_data['school_id']}' AND status = 1");
					$result['result'][$key]['no_use_code'] = $no_use_code['count'];
					$dongjie = get_field_by_where("o2o_school_system_topic_codes", "count(id) AS count", "status=1 AND dongjie=1 AND subject_id = '{$value['id']}' AND school_id = '{$this->user_data['school_id']}'");
					$result['result'][$key]['dongjie'] = $dongjie['count'];
				}
			}
			$this->load->library('pagination');
			$page_config['base_url'] = '/subject/index/' . $title . '/' . $tk_category_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->load->model("Tk_categorys");
			$category_list = $this->Tk_categorys->get_top_categorys($this->user_data['school_id']);
			$this->smarty->assign("category_list", $category_list);
			$this->smarty->assign("action", '科目列表');
			$this->smarty->assign("menu", 'shelves');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("subject_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("subject/system_subject.html");
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
				$post_data['create_on'] = time();
				$post_data['school_id'] = $this->user_data['school_id'];
				$this->db->insert("b_subject", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				$this->load->model("Tk_categorys");
				$category_list = $this->Tk_categorys->get_tk_category_list(0, 0, array("school_id" => $this->user_data['school_id']));
				$this->smarty->assign("category_list", $category_list['result']);
				$this->smarty->assign("action", '添加题库');
				$this->smarty->assign("post_url", '/subject/add');
				$this->smarty->display("subject/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
				$this->db->where('id', $post_data['id']);
				$this->db->update('b_subject', $post_data);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				if(intval($id) > 0){
					$subject_info = get_field_by_where("b_subject", "*", "id='{$id}' AND school_id = '{$this->user_data['school_id']}'");
					if(!$subject_info){
						show_message($this->jump_url, "数据不存在");exit;
					}
					$this->load->model("Tk_categorys");
					$category_list = $this->Tk_categorys->get_tk_category_list(0, 0, array("school_id" => $this->user_data['school_id']));
					$this->smarty->assign("subject_info", $subject_info);
					$this->smarty->assign("category_list", $category_list['result']);
					$this->smarty->assign("post_url", '/subject/edit');
					$this->smarty->assign("action", '编辑科目');
					$this->smarty->display("subject/save.html");
				}else{
					show_message($this->jump_url, "非法操作");
				}
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "b_subject");
			if($this->db->affected_rows() > 0){
				show_message($this->jump_url, "删除成功");
			}else{
				show_message($this->jump_url, "删除失败，请重试！");
			}
		}

		 function chapter_list($subject_id = 0, $parent_id = 0, $title = 0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
                $parent_id = !empty($post_data['parent_id']) ? $post_data['parent_id'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('subject_id' => $subject_id, 'parent_id' => $parent_id, 'title' => $title, "school_id" => $this->user_data['school_id']);
            $this->load->model("Subjects");
            $result = $this->Subjects->get_chapter_list($limit, $this->offset, $conditions);
            $this->load->library('pagination');
            $page_config['base_url'] = '/subject/chapter_list/' . $subject_id . '/' . $parent_id . '/' . $title;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 6;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();
            $subject_list = $this->Subjects->get_subject_list(0,0,$conditions);
            $this->smarty->assign("subject_list", $subject_list['result']);
            $this->smarty->assign("action", '章节列表');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("chapter_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->display("subject/chapter_list.html");
        }

		/*function chapter_list($subject_id = 0, $parent_id = 0, $title = 0, $limit = 0){
			// if($this->input->post()){
			// 	$limit = 0;
			// 	$post_data = $this->input->post();
			// 	$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
			// 	$subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
			// 	$parent_id = !empty($post_data['parent_id']) ? $post_data['parent_id'] : 0;
			// }
			// $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			// $conditions = array('subject_id' => $subject_id, 'parent_id' => $parent_id, 'title' => $title, "school_id" => $this->user_data['school_id']);
			// $this->load->model("Subjects");
			// $result = $this->Subjects->get_chapter_list($limit, $this->offset, $conditions);
			
			// $this->load->library('pagination');
			// $page_config['base_url'] = '/subject/chapter_list/' . $subject_id . '/' . $parent_id . '/' . $title;
			// $page_config['total_rows'] = $result['count'];
			// $page_config['per_page'] = $this->offset;
			// $page_config['uri_segment'] = 6;
			// $this->pagination->initialize($page_config);
			// $pagination = $this->pagination->create_links();
			// $subject_list = $this->Subjects->get_subject_list(0, 0, array("school_id" => $this->user_data['school_id']));
			// $this->smarty->assign("subject_list", $subject_list['result']);
			// $this->smarty->assign("action", '科目章节列表');
			// $this->smarty->assign("conditions", $conditions);
			// $this->smarty->assign("current_url", $current_url);
			// $this->smarty->assign("chapter_list", $result['result']);
			// $this->smarty->assign("pagination", $pagination);
			$this->load->model("Subject_chapter_model");
			$chapter_list = $this->Subject_chapter_model->get_chapter($subject_id);
			print_r($chapter_list);exit;

			$this->load->model("Subjects");
			$subject_list = $this->Subjects->get_subject_list(0, 0, array("school_id" => $this->user_data['school_id']));
			$this->smarty->assign("subject_list", $subject_list['result']);
			
			
			$this->smarty->assign("action", '科目章节列表');
			$this->smarty->assign("conditions", '');
			$this->smarty->assign("current_url", '');
			$this->smarty->assign("chapter_list", $chapter_list);
			$this->smarty->assign("pagination", '');
			$this->smarty->display("subject/chapter_list.html");
		}*/

		function add_chapter(){
			$post_data = $this->input->post();
			if($post_data){
				$post_data['create_on'] = time();
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				$this->db->insert("b_subject_chapter", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				$this->load->model("Subjects");
				$conditions['school_id'] = $this->user_data['school_id'];

				$subject_list = $this->Subjects->get_subject_list(0,10,$conditions);

				$this->smarty->assign("subject_list", $subject_list['result']);
				$this->smarty->assign("chapter_list_data",  array());
				$this->smarty->assign("action", '添加题库');
				$this->smarty->assign("post_url", '/subject/add_chapter');
				$this->smarty->display("subject/chapter_save.html");
			}
		}

		function edit_chapter($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				$this->db->where('id', $post_data['id']);
				$this->db->update('b_subject_chapter', $post_data);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				if(intval($id) > 0){
					$chapter_info = get_field_by_where("b_subject_chapter", "*", "id='{$id}'");
					if(!$chapter_info){
						show_message($this->jump_url, "数据不存在");exit;
					}
					$this->load->model("Subjects");
					$chapter_list_data = $this->get_chapter_list($chapter_info['subject_id']);
					
					$subject_list = $this->Subjects->get_subject_list_all($this->user_data['school_id']);
					$this->smarty->assign("subject_list", $subject_list);
					$this->smarty->assign("chapter_list_data", $chapter_list_data);
					$this->smarty->assign("chapter_info", $chapter_info);
					
					$this->smarty->assign("post_url", '/subject/edit_chapter');
					$this->smarty->assign("action", '编辑科目');
					$this->smarty->display("subject/chapter_save.html");
				}else{
					show_message($this->jump_url, "非法操作");
				}
			}
		}

		function get_chapter_list($subject_id){
			$this->load->model("Topics");
			$conditions['subject_id'] = $subject_id;
			$chapter_list = $this->Topics->get_chapter($conditions);
			$chapter_list_data = array();
			$code = 0;
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if($value['sub']){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			return $chapter_list_data;
		}

		function delete_chapter($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "b_subject_chapter");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

		function shelves($subject_id = 0, $pack_id = 0){
			$post_data = $this->input->post();
			if($post_data){
				$subject_info = get_field_by_where("b_subject", "*", "id='{$post_data['subject_id']}'");
				if(!$subject_info){
					show_message($_SERVER['HTTP_REFERER'], "科目不存在");
				}else{
					$where = "school_id='{$this->user_data['school_id']}' AND subject_id='{$post_data['subject_id']}'";
					$shelves_info = get_field_by_where("o2o_school_tk_packages", "id", $where);
					if($shelves_info){
						// echo json_encode(array("code" => 0));
						$post_data['status'] = 1;
						$this->db->where("id", $shelves_info['id']);
						$this->db->update("o2o_school_tk_packages", $post_data);
						echo json_encode(array("code" => 2));
					}else{
						$shelves_data['tk_category_id'] = $post_data['tk_category_id'];
						$shelves_data['subject_id'] = $post_data['subject_id'];
						$shelves_data['type'] = ($subject_info['school_id'] == 1) ? 1 : 2;
						$shelves_data['school_id'] = $this->user_data['school_id'];
						$shelves_data['price'] = $post_data['price'];
						$shelves_data['status'] = 1;
						$shelves_data['commission'] = $post_data['commission'];
						$shelves_data['expire_day'] = $post_data['expire_day'];
						$shelves_data['on_time'] = time();
						$this->db->insert("o2o_school_tk_packages", $shelves_data);
						echo json_encode(array("code" => 1));
					}
				}
			}else{
				if($pack_id > 0){
					$pack_info = get_field_by_where("o2o_school_tk_packages", "*", "id='{$pack_id}'");
					$this->smarty->assign("pack_info", $pack_info);
					$school_order_info = get_field_by_where("o2o_school_order_records", "*", "school_id='{$this->user_data['school_id']}' AND data_id='{$subject_id}' AND data_type = 4 AND reviewed = 1 ORDER BY id DESC");
					$this->smarty->assign("school_order_info", $school_order_info);
				}
				$code_info = get_field_by_where("o2o_school_system_topic_codes", "*", "school_id='{$this->user_data['school_id']}' AND subject_id='{$subject_id}' AND use_uid <= 0 AND status = 1 ORDER BY expire_day DESC LIMIT 1");
				$this->load->model("Subjects");
				$category_list = $this->Subjects->get_school_subject_category();
				$this->smarty->assign("code_info", $code_info);
				$this->smarty->assign("category_list", $category_list);
				$this->smarty->assign("action", '授权');
				$this->smarty->assign("subject_id", $subject_id);
				$this->smarty->assign("pack_id", $pack_id);
				$this->smarty->display("subject/shelves.html");
			}
		}

		function shelves_down($pack_id){
			$this->db->where("id", $pack_id);
			$this->db->update("o2o_school_tk_packages", array("status" => 0));
			echo json_encode(array("code" => 1));
		}

		function update_sort(){
			$sort = $this->input->post('sort');
			if(is_array($sort) && count($sort) > 0){
				foreach($sort as $key=>$val) {
					$this->db->where('id', $key);
 					$this->db->update('b_subject_chapter', array('sort'=>$val));
				}
			}
			show_message($_SERVER['HTTP_REFERER'], "操作成功");
		}
	}